<template>
  <div>
    <div class="app-title">
      <span>级别配置</span>
    </div>
    <!-- 按钮 -->
    <div class="filter-container">
      <div>
        <el-col style="text-align:right;margin-top: -8px;">
          <el-button
            size="small"
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            @click="handleAddLevel"
          >
            新增级别
          </el-button>
          <el-button
            size="small"
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            @click="handleEditLevel"
          >
            修改级别
          </el-button>
        </el-col>
      </div>
    </div>
    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :height="tableHeight"
      :data="list"
      :stripe="true"
      fit
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column type="index" align="center" width="50" label="序号" />

      <el-table-column label="人员类别" align="center">
        <template slot-scope="{row}">
          <span>{{ row.personTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="职务" align="center">
        <template slot-scope="{row}">
          <span>{{ row.postName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="级别编码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="级别名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!-- 新增/编辑页面部分 -->
    <el-dialog  v-dialogDrag  
      :close-on-click-modal="false"
      :title="dialogStatus"
      :visible.sync="dialogFormVisible"
      width="20%"
    >
      <el-form
        ref="dataForm"
        class="dialog-backgroundcolor"
        :rules="dataFormRules"
        :model="formData"
        label-position="top"
        label-width="120px"
        size="small"
      >
        <div class="formDeviation">
          <el-row>
            <el-col>
              <el-form-item
                label="人员类别："
                prop="personTypeId"
                class="item-margin"
              >
                <el-select v-model="formData.personTypeId" placeholder="请选择" :disabled="isEdit" @change="queryPostList">
                  <el-option
                    v-for="item in personTypeList"
                    :key="item.id"
                    :label="item.code+'-'+item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item
                label="职务："
                prop="postId"
                class="item-margin"
              >
                <el-select v-model="formData.postId" placeholder="请选择" :disabled="isEdit">
                  <el-option
                    v-for="item in postList"
                    :key="item.id"
                    :label="item.code+'-'+item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item
                label="编码："
                prop="code"
                class="item-margin"
              >
                <el-input
                  v-model="formData.code"
                  :disabled="isEdit"
                  maxlength="10"
                /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item
                style="margin-top:10px"
                label="名称："
                prop="name"
                class="item-margin"
              >
                <el-input
                  v-model="formData.name"
                  maxlength="200"
                /></el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col
            style="text-align: center; margin-top:20px; margin-bottom:8px"
          >
            <el-button
              type="primary"
              :loading="saveLoading"
              @click="updateData()"
            >
              保存
            </el-button>
            <el-button @click="dialogFormVisible = false">
              取消
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { querySalPostByPersonTypeId, querySalPersonTypeAll } from '@/api/salaryConfigManage/personTypePostConfig'
import { querySalPostGradeLevelPage, saveSalPostGradeLevel, updateSalPostGradeLevel, queryDataByCode } from '@/api/salaryConfigManage/gradeLevelConfig'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  name: 'PostChildren',
  components: { Pagination, tableEmpty },
  filters: {
  },
  data() {
    return {
      tableHeight: null,
      that: this,
      list: null,
      total: 0,
      listLoading: false,
      saveLoading: false,
      deleteLoading: false,
      listQuery: {
        page: 1,
        limit: 200,
        type: 'level'
      },
      formData: {
        id: '',
        personTypeId: '',
        postId: '',
        type: 'level',
        code: '',
        name: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      isEdit: false,
      dataFormRules: {
        personTypeId: [
          { required: true, message: '请输入人员类别', trigger: 'blur' }
        ],
        postId: [
          { required: true, message: '请输入职务', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      personTypeList: [],
      postList: []
    }
  },
  watch: {
    // 表格的数据数组
    list(val) {
      this.doLayout()
    }
  },
  created() {
    this.getList()
    this.initBaseData()
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  methods: {
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 220
    },
    getList() {
      this.listLoading = true
      querySalPostGradeLevelPage(this.listQuery).then((response) => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 新增级别
    handleAddLevel() {
      this.dialogStatus = '新增级别'
      this.formData = {
        id: '',
        personTypeId: '',
        postId: '',
        type: 'level',
        code: '',
        name: ''
      }
      this.isEdit = false
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 编辑
    handleEditLevel() {
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          showClose: true,
          message: '请选择要编辑数据',
          type: 'warning',
          duration: 4000
        })
        return
      }
      this.isEdit = true
      this.dialogStatus = '编辑级别'
      this.dialogFormVisible = true
      this.formData = Object.assign({}, this.formData, selection[0])
      this.queryPostList('edit')
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 保存
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          const tempData = Object.assign({}, this.formData)
          if (tempData.id === '') { // 新增
            const params = { 'type': this.formData.type, 'code': this.formData.code }
            queryDataByCode(params).then((response) => {
              if (response.data === 0) {
                saveSalPostGradeLevel(tempData).then((response) => {
                  this.dialogFormVisible = false
                  this.$message({
                    showClose: true,
                    message: '保存成功',
                    type: 'success',
                    duration: 4000
                  })
                  this.saveLoading = false
                  this.handleFilter()
                }).catch(() => {
                  this.saveLoading = false
                })
              } else {
                this.$message({
                  showClose: true,
                  message: '编码重复',
                  type: 'warning',
                  duration: 4000
                })
                this.saveLoading = false
              }
            })
          } else { // 修改
            updateSalPostGradeLevel(tempData).then((response) => {
              this.dialogFormVisible = false
              this.$message({
                showClose: true,
                message: '编辑成功',
                type: 'success',
                duration: 4000
              })
              this.saveLoading = false
              this.handleFilter()
            }).catch(() => {
              this.saveLoading = false
            })
          }
        }
      })
    },
    // 查询职务信息
    queryPostList(type) {
      if (type !== 'edit') {
        this.formData.postId = ''
      }
      const params = { 'personTypeId': this.formData.personTypeId }
      querySalPostByPersonTypeId(params).then((response) => {
        this.postList = Object.assign(response.data)
      })
    },
    initBaseData() {
      querySalPersonTypeAll().then((response) => {
        this.personTypeList = Object.assign(response.data)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.formDeviation {
  margin-left: 18%;
  .el-form-item__error {
    padding-top: 5%;
  }
}
.redstar.el-form-item__error{
  margin-top: 35px !important;
  left: -96px !important;
}
</style>
